﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.WebPages;
using System.Web.Helpers;
using System.Web.Mvc;

namespace System.Web.Helpers
{
    
    /// <summary>
    /// Usage: 
    /// <fieldset>
    //    <legend>Person</legend>
    //    @grid.GetHtmlWithSelectAllCheckBox(
    //        tableStyle: "grid", checkBoxValue: "ID",
    //        columns: grid.Columns(
    //            grid.Column(columnName: "Name"),
    //            grid.Column(columnName: "Email"),
    //            grid.Column(columnName: "Adress")
    //    ))
    //    <p>
    //        <input type="submit" value="Save" />
    //    </p>
    //</fieldset>
    /// </summary>
    public static class WebGridExt
    {
        public static IHtmlString GetHtmlWithSelectAllCheckBox(
            this WebGrid webGrid, string tableStyle = null,
string headerStyle = null, string footerStyle = null, string rowStyle = null,
  string alternatingRowStyle = null, string selectedRowStyle = null,
  string caption = null, bool displayHeader = true, bool fillEmptyRows = false,
  string emptyRowCellValue = null, IEnumerable<WebGridColumn> columns = null,
  IEnumerable<string> exclusions = null, WebGridPagerModes mode = WebGridPagerModes.All,
  string firstText = null, string previousText = null, string nextText = null,
  string lastText = null, int numericLinksCount = 5, object htmlAttributes = null,
  string checkBoxValue = "ID")
        {

            var newColumn = webGrid.Column(header: "{}",
            format: item => new HelperResult(writer =>
            {
                writer.Write("<input class=\"singleCheckBox\" name=\"selectedRows\" value=\""
                + item.Value.GetType().GetProperty(checkBoxValue).GetValue(item.Value, null).ToString()
                + "\" type=\"checkbox\" />"
                );
            }));

            var newColumns = columns.ToList();
            newColumns.Insert(0, newColumn);

            var script = @"<script>
                
                if (typeof jQuery == 'undefined')
                {
                    document.write(
                        unescape(
                        ""%3Cscript src='http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js'%3E%3C/script%3E""
                        )
                     );
                }
                
                $(document).ready(function () {
                    $('#allCheckBox').click(function () {
                        var isChecked = $(this).attr('checked');                        
                        $('.singleCheckBox').attr('checked', isChecked  ? true: false);
                        $('.singleCheckBox').closest('tr').addClass(isChecked  ? 'selected-row': 'not-selected-row');
                        $('.singleCheckBox').closest('tr').removeClass(isChecked  ? 'not-selected-row': 'selected-row');
                    });

                    $('.singleCheckBox').click(function () {
                        var isChecked = $(this).attr('checked');
                        $(this).closest('tr').addClass(isChecked  ? 'selected-row': 'not-selected-row');
                        $(this).closest('tr').removeClass(isChecked  ? 'not-selected-row': 'selected-row');
                        if(isChecked && $('.singleCheckBox').length == $('.selected-row').length){
                            $('#allCheckBox').attr('indeterminate', false);
                            $('#allCheckBox').attr('checked',true);
                        }
                        else{
                            $('#allCheckBox').attr('checked',false);
                            $('#allCheckBox').attr('indeterminate', $('.selected-row').length > 0);
                        }
                    });
                });
            </script>";

            var html = webGrid.GetHtml(tableStyle, headerStyle, footerStyle, rowStyle,
                                    alternatingRowStyle, selectedRowStyle, caption,
                                    displayHeader, fillEmptyRows, emptyRowCellValue,
                                    newColumns, exclusions, mode, firstText,
                                    previousText, nextText, lastText,
                                    numericLinksCount, htmlAttributes
                                    );

            return MvcHtmlString.Create(html.ToString().Replace("{}",
                      "<input type='checkbox' id='allCheckBox'/>") + script);
        }
    }
}